package com.ruoyi.exam.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.TPointsRules;
import com.ruoyi.exam.service.ITPointsRulesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分规则Controller
 * 
 * @author ruoyi
 * @date 2025-01-13
 */
@RestController
@RequestMapping("/exam/rules")
public class TPointsRulesController extends BaseController
{
    @Autowired
    private ITPointsRulesService tPointsRulesService;

    /**
     * 查询积分规则列表
     */
    @PreAuthorize("@ss.hasPermi('exam:rules:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPointsRules tPointsRules)
    {
        startPage();
        List<TPointsRules> list = tPointsRulesService.selectTPointsRulesList(tPointsRules);
        return getDataTable(list);
    }

    /**
     * 导出积分规则列表
     */
    @PreAuthorize("@ss.hasPermi('exam:rules:export')")
    @Log(title = "积分规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TPointsRules tPointsRules)
    {
        List<TPointsRules> list = tPointsRulesService.selectTPointsRulesList(tPointsRules);
        ExcelUtil<TPointsRules> util = new ExcelUtil<TPointsRules>(TPointsRules.class);
        util.exportExcel(response, list, "积分规则数据");
    }

    /**
     * 获取积分规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:rules:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tPointsRulesService.selectTPointsRulesById(id));
    }

    /**
     * 新增积分规则
     */
    @PreAuthorize("@ss.hasPermi('exam:rules:add')")
    @Log(title = "积分规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPointsRules tPointsRules)
    {
        return toAjax(tPointsRulesService.insertTPointsRules(tPointsRules));
    }

    /**
     * 修改积分规则
     */
    @PreAuthorize("@ss.hasPermi('exam:rules:edit')")
    @Log(title = "积分规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPointsRules tPointsRules)
    {
        return toAjax(tPointsRulesService.updateTPointsRules(tPointsRules));
    }

    /**
     * 删除积分规则
     */
    @PreAuthorize("@ss.hasPermi('exam:rules:remove')")
    @Log(title = "积分规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tPointsRulesService.deleteTPointsRulesByIds(ids));
    }
    @PreAuthorize("@ss.hasPermi('exam:rules:rank')")
    @GetMapping("/rank/{userId}")
    public Map<String, Object> getUserTotalPointsAndRank(@PathVariable Long userId) {
        return tPointsRulesService.getUserTotalPointsAndRank(userId);
    }
}
